let app = getApp();
Page({
    data: {
        region: ['河南省', '洛阳市', '洛龙区'],
        userInfo:{}
    },
    onLoad: function (options) {
        let that = this;
        wx.request({
            url: app.globalData.serverUrl + "user/" + app.globalData.userInfo.id,
            method: 'get',
            success: function (res) {
                if (res.data.status == 200)
                console.log(res.data.data)
                that.setData({
                    userInfo: res.data.data
                });
            }
        })

        console.log(this.userInfo)
    },
    onshow: function () {

    },
    update: function () {
        wx.navigateTo({
            url: '../update-user/update-user'
        })

    },
    changeHeadPic: function () {
        let that = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success (res) {
                // tempFilePath可以作为img标签的src属性显示图片
                const tempFilePaths = res.tempFilePaths
                console.log(tempFilePaths[0])
                wx.uploadFile({
                    url: app.globalData.serverUrl + 'qiniu', //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        'user': 'test'
                    },
                    success (res){
                        let data = JSON.parse(res.data)
                        //do something
                        if (data.status == 200){
                            that.setData({
                                userInfo: {
                                    id: app.globalData.userInfo.id,
                                    nickname: that.data.nickname,
                                    enrollmentYear: that.data.enrollmentYear,
                                    university: that.data.university,
                                    college: that.data.college,
                                    email: that.data.email,
                                    phone: that.data.phone,
                                    major: that.data.major,
                                    headPic: data.data.link
                                }
                            });
                            wx.request({
                                url: app.globalData.serverUrl + 'user/',
                                method: 'put',
                                data: that.data.userInfo,
                                headers:{
                                    'content-type':'application/json'
                                },
                                success: function (res) {
                                    if (res.data.status == 200) {
                                        wx.showToast({
                                            title: res.data.data.success,
                                            icon: 'none',
                                            duration: 1500
                                        });
                                        that.onLoad();
                                    }
                                }
                            })
                        }
                    }
                })
            }
        })
    }
});
